# coding: utf-8
import decimal
import hashlib
import json
import math
import re
import string
import subprocess
import time
import zipfile
from decimal import Decimal
import datetime
# from itsdangerous import TimedJSONWebSignatureSerializer as Serializer, SignatureExpired, \
#     BadSignature
import random
from docx import Document
import requests
from app.libs.TextRank.textRank import TextRank
from app.config.secure import *
from app.models.document_dom import DocumentDom


def conditions(form):
    conditions = {}
    pop_keys = ['page', 'limit', 'id', 'start', 'end', 'password', 'shop_list', 'task_files', 'type']
    if hasattr(form, 'pop_fields'):
        pop_keys += form.pop_fields
    for key, value in form.data.items():
        if key not in pop_keys and value != '' and value is not None:
            conditions[key] = value
    return conditions


def sql_conditions(form):
    conditions = {}
    pop_keys = []
    if hasattr(form, 'pop_fields'):
        pop_keys += form.pop_fields
    for key, value in form.data.items():
        if key not in pop_keys and value != '' and value is not None:
            conditions[key] = value
    return conditions


def number2string(number):
    sum = str(Decimal(number).quantize(Decimal('0.00')))
    return sum


def flaot2Decimal(number):
    a = Decimal.from_float(number).quantize(Decimal('0.00'))
    return a


def getYesterday():
    today = datetime.date.today()
    oneday = datetime.timedelta(days=1)
    yesterday = today - oneday
    return str(yesterday)


def signstr(pre_body, appsecret):  # 签名
    sign_str = appsecret
    for key, value in pre_body.items():
        sign_str += key + value
    sign_str += appsecret
    md5 = hashlib.md5()
    md5.update(sign_str.encode('utf-8'))
    return md5.hexdigest()


def time2int(timestr):
    timeArray = time.strptime(timestr, "%Y-%m-%d %H:%M:%S")
    timeStamp = int(time.mktime(timeArray))
    return timeStamp


# @api.route('/secret', methods=['POST'])
# def get_token_info(token):
#     """获取令牌信息"""
#     # form = TokenForm().validate_for_api()
#     s = Serializer('63505711')
#     try:
#         data = s.loads(token, return_header=True)
#     except SignatureExpired:
#         return False
#     except BadSignature:
#         return False
#
#     r = {
#         'scope': data[0]['scope'],
#         'create_at': data[1]['iat'],
#         'expire_in': data[1]['exp'],
#         'uid': data[0]['uid']
#     }
#     return r


def generate_verification_code(len=6):
    ''' 随机生成6位的验证码 '''
    # 注意： 这里我们生成的是0-9A-Za-z的列表，当然你也可以指定这个list，这里很灵活
    # 比如： code_list = ['P','y','t','h','o','n','T','a','b'] # PythonTab的字母
    code_list = []
    for i in range(10):  # 0-9数字
        code_list.append(str(i))
    myslice = random.sample(code_list, len)  # 从list中随机获取6个元素，作为一个片断返回
    verification_code = ''.join(myslice)  # list to string
    return verification_code


def first_day_of_month():
    today = datetime.date.today()
    first = today.replace(day=1)
    return int(time.mktime(time.strptime(str(first), "%Y-%m-%d")))


def timerisk(deadline, create_time):
    try:
        if int(time.time()) - deadline > 0:
            costtime = 0
        else:
            costtime = (deadline - int(time.time())) / (deadline - create_time)
    except:
        costtime = 0
    return costtime


def get_shop_list(shop_list):
    list = []
    for shop in shop_list:
        list.append(shop.shop_id)
    return list


def get_user_list(shop_list):
    list = []
    for shop in shop_list:
        list.append(shop.user_id)
    return list


def float2decimal(amount):
    decimal_number = decimal.Decimal(str(amount))
    # 量化为小数点后2位
    quantized_decimal = decimal_number.quantize(decimal.Decimal('0.01'))
    return quantized_decimal


def generate_order_id():
    # 获取当前时间戳（精确到毫秒）
    timestamp = int(time.time() * 1000)

    # 生成一个随机数（16位）
    random_num = random.randint(10 ** 15, 10 ** 16 - 1)

    # 将时间戳和随机数合并成一个字符串
    order_id = f"{timestamp}{random_num}"

    # 如果订单号长度不足32位，用随机数补齐
    if len(order_id) < 32:
        order_id += ''.join([str(random.randint(0, 9)) for _ in range(32 - len(order_id))])

    # 截取前32位
    return order_id[:32]


def get_tasktype_list(filetypes):
    list = []
    for filetype in filetypes:
        list.append(filetype.id)
    return list


def get_shop_string(shop_list):
    list = []
    for shop in shop_list:
        list.append(shop.shop_name)
    str = '，'.join(list)
    return str


# 提取摘要
def collect_content(desc):
    pattern = r'<.*?>|&nbsp;|\n'
    res = re.sub(pattern, ' ', desc)
    collect = res[0:100]
    return collect


def bytes2human(n):
    symbols = ('K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y')
    prefix = {}
    for i, s in enumerate(symbols):
        prefix[s] = 1 << (i + 1) * 10
    for s in reversed(symbols):
        if n >= prefix[s]:
            value = float(n) / prefix[s]
            return '%.1f%s' % (value, s)
    return '%sB' % n


def create_nickname():
    randomStr = ""
    for i in range(6):
        temp = random.randrange(0, 3)
        if temp == 0:
            ch = chr(random.randrange(ord('A'), ord('Z') + 1))
            randomStr += ch
        elif temp == 1:
            ch = chr(random.randrange(ord('a'), ord('z') + 1))
            randomStr += ch
        else:
            ch = str((random.randrange(0, 10)))
            randomStr += ch
    return randomStr


def paragraphModelsToDict(paragrapshs):
    list = []
    for paragraph in paragrapshs:
        info = {
            'id': paragraph.id,
            'text': paragraph.text,
            'node': paragraph.node,
            'path': paragraph.path,
            'index': paragraph.index,
            'level': paragraph.level
        }
        list.append(info)
    return list


def check_paragrpah(paragraphs):
    uncheck = []
    paragraphList = []
    abstract_sign = 0
    # paragraphsList = paragraphModelsToDict(paragraphs)
    cankan_sign = paragraphs[-1].id

    for paragraph in paragraphs:
        zh_count, zh_content, english_words = count_zh(paragraph.text)
        if zh_content[0:2] == '摘要':
            abstract_sign = paragraph.id
        if zh_content == '参考文献':
            cankan_sign = paragraph.id
    for index, paragraph in enumerate(paragraphs):
        zh_count, zh_content, english_words = count_zh(paragraph.text)
        if zh_content in uncheck:
            paragraphList.append({
                'content': paragraph.text,
                'path': paragraph.path,
                'check': 0
            })
            continue
        if zh_count <= 10 and len(paragraph.text.strip()) < 12 and zh_count > 0:
            paragraphList.append({
                'content': paragraph.text,
                'path': paragraph.path,
                'check': 0
            })
            uncheck.append(zh_content)
            continue
        if zh_count <= 300 and '本人郑重申明' in zh_content:
            paragraphList.append({
                'content': paragraph.text,
                'path': paragraph.path,
                'check': 0
            })
            uncheck.append(zh_content)
            continue
        if zh_count <= 300 and '本人声明所呈交' in zh_content:
            paragraphList.append({
                'content': paragraph.text,
                'path': paragraph.path,
                'check': 0
            })
            uncheck.append(zh_content)
            continue
        if zh_count <= 300 and '本人郑重声明' in zh_content:
            paragraphList.append({
                'content': paragraph.text,
                'path': paragraph.path,
                'check': 0
            })
            uncheck.append(zh_content)
            continue
        if zh_count <= 300 and '本学位论文作者完全了解' in zh_content:
            paragraphList.append({
                'content': paragraph.text,
                'path': paragraph.path,
                'check': 0
            })
            uncheck.append(zh_content)
            continue
        if zh_count <= 300 and '本人完全了解' in zh_content:
            paragraphList.append({
                'content': paragraph.text,
                'path': paragraph.path,
                'check': 0
            })
            uncheck.append(zh_content)
            continue
        if zh_count <= 300 and '学位作者完全了解' in zh_content:
            paragraphList.append({
                'content': paragraph.text,
                'path': paragraph.path,
                'check': 0
            })
            uncheck.append(zh_content)
            continue
            # 我承诺对本人填写的各项内容的真实性负责
        if zh_count <= 300 and '我承诺对本人填写的各项内容的真实性负责' in zh_content:
            paragraphList.append({
                'content': paragraph.text,
                'path': paragraph.path,
                'check': 0
            })
            uncheck.append(zh_content)
            continue
        if zh_count <= 300 and '有权保存学位论文的印刷本和电子版' in zh_content:
            paragraphList.append({
                'content': paragraph.text,
                'path': paragraph.path,
                'check': 0
            })
            uncheck.append(zh_content)
            continue
        if zh_count <= 300 and '作为申请学位的条件之一' in zh_content:
            paragraphList.append({
                'content': paragraph.text,
                'path': paragraph.path,
                'check': 0
            })
            uncheck.append(zh_content)
            continue
        if zh_count <= 300 and '呈交的学位论文' in zh_content:
            paragraphList.append({
                'content': paragraph.text,
                'path': paragraph.path,
                'check': 0
            })
            uncheck.append(zh_content)
            continue
        if '姓名' in zh_content and zh_count < 10:
            paragraphList.append({
                'content': paragraph.text,
                'path': paragraph.path,
                'check': 0
            })
            uncheck.append(zh_content)
            continue
        if '评分标准' in zh_content and zh_count < 15:
            paragraphList.append({
                'content': paragraph.text,
                'path': paragraph.path,
                'check': 0
            })
            uncheck.append(zh_content)
            continue
        if '指导教师' in zh_content and zh_count < 12:
            paragraphList.append({
                'content': paragraph.text,
                'path': paragraph.path,
                'check': 0
            })
            uncheck.append(zh_content)
            continue
        if '题目' in zh_content and zh_count < 40:
            paragraphList.append({
                'content': paragraph.text,
                'path': paragraph.path,
                'check': 0
            })
            uncheck.append(zh_content)
            continue
        if '学院' in zh_content and zh_count < 15:
            paragraphList.append({
                'content': paragraph.text,
                'path': paragraph.path,
                'check': 0
            })
            uncheck.append(zh_content)
            continue
        if '专业班级' in zh_content and zh_count < 15:
            paragraphList.append({
                'content': paragraph.text,
                'path': paragraph.path,
                'check': 0
            })
            uncheck.append(zh_content)
            continue
        if '学号' in zh_content and zh_count < 10:
            paragraphList.append({
                'content': paragraph.text,
                'path': paragraph.path,
                'check': 0
            })
            uncheck.append(zh_content)
            continue
        if '职称' in zh_content and zh_count < 10:
            paragraphList.append({
                'content': paragraph.text,
                'path': paragraph.path,
                'check': 0
            })
            uncheck.append(zh_content)
            continue
        if '课题名称' in zh_content and zh_count < 35:
            paragraphList.append({
                'content': paragraph.text,
                'path': paragraph.path,
                'check': 0
            })
            uncheck.append(zh_content)
            continue
        if '课程名称' in zh_content and zh_count < 35:
            paragraphList.append({
                'content': paragraph.text,
                'path': paragraph.path,
                'check': 0
            })
            uncheck.append(zh_content)
            continue
        if '专业' in zh_content and zh_count < 11:
            paragraphList.append({
                'content': paragraph.text,
                'path': paragraph.path,
                'check': 0
            })
            uncheck.append(zh_content)
            continue
        if '学科分类' in zh_content and zh_count < 20:
            paragraphList.append({
                'content': paragraph.text,
                'path': paragraph.path,
                'check': 0
            })
            uncheck.append(zh_content)
            continue
        if '所在单位' in zh_content and zh_count < 25:
            paragraphList.append({
                'content': paragraph.text,
                'path': paragraph.path,
                'check': 0
            })
            uncheck.append(zh_content)
            continue
        if '关键词' in zh_content and zh_count < 20:
            paragraphList.append({
                'content': paragraph.text,
                'path': paragraph.path,
                'check': 0
            })
            uncheck.append(zh_content)
            continue
        if paragraph.id < abstract_sign and zh_count < 150:
            paragraphList.append({
                'content': paragraph.text,
                'path': paragraph.path,
                'check': 0
            })
            uncheck.append(zh_content)
            continue
        if paragraph.id > cankan_sign and (check_doc_str(paragraph.text)) or (
                re.search('([\[|【|［]\d{1,3}[】|\]|］])', paragraph.text.strip()[0:4])) and len(
            paragraph.text) < 140:
            paragraphList.append({
                'content': paragraph.text,
                'path': paragraph.path,
                'check': 0
            })
            uncheck.append(zh_content)
            continue
        if paragraph.node == 'span':
            if len(paragraph.text) <= 10 and ';' not in paragraph.text.strip() and '；' not in paragraph.text.strip():
                paragraphList.append({
                    'content': paragraph.text,
                    'path': paragraph.path,
                    'check': 0
                })
                uncheck.append(zh_content)
                continue
            check_attrs = CheckPNodeAttrs(paragraph)
            if check_attrs:
                if re.search(
                        '(([(（]){1}(一|二|三|四|五|六|七|八|九|\d{1,2})([)）]){1})|((一|二|三|四|五|六|七|八|九|\d{1,2})(.|。|,| ){1})',
                        paragraph.text.strip()) and zh_count < 20 and ';' not in paragraph.text.strip() and '；' not in paragraph.text.strip():
                    paragraphList.append(
                        {
                            'content': paragraph.text,
                            'path': paragraph.path,
                            'check': 0
                        }
                    )
                    uncheck.append(zh_content)
                    continue
                if len(paragraph.text) <= 55 and zh_count > 0 and len(paragraph.text) > 10:
                    paragraphList.append({
                        'content': paragraph.text,
                        'path': paragraph.path,
                        'check': 1
                    })
                    continue
                if len(paragraph.text) > 55 and zh_count > 0:
                    if check_attrs:
                        sentence_list = SplitSentences('([。！；;])', paragraph.text)
                        for sentence_list_item in sentence_list:
                            if len(sentence_list_item) > 55:
                                sub_sentences_list = SplitSentences('([,，])', sentence_list_item)
                                paragraph_sentence_list = merge_sub_sentence(sub_sentences_list)
                                for paragraph_sentence_list_item in paragraph_sentence_list:
                                    paragraphList.append(
                                        {
                                            'content': paragraph_sentence_list_item,
                                            'path': paragraph.path,
                                            'check': 1
                                        }
                                    )
                            else:
                                if len(sentence_list_item) > 0:
                                    paragraphList.append(
                                        {
                                            'content': sentence_list_item,
                                            'path': paragraph.path,
                                            'check': 1
                                        }
                                    )
            else:
                paragraphList.append({
                    'content': paragraph.text,
                    'path': paragraph.path,
                    'check': 0
                })
                uncheck.append(zh_content)
        if paragraph.node == 'tr':
            paragraphList.append({
                'content': paragraph.text,
                'path': paragraph.path,
                'check': 1
            })
    return paragraphList


def parentPath(paragraph):
    currentNode = f'-{paragraph.node}_{paragraph.index}'
    parentPath = paragraph.path.split(currentNode)[0]
    return parentPath


def CheckPNodeAttrs(paragraph):
    zh_count, zh_content, english_words = count_zh(paragraph.text)
    Pnode = DocumentDom.query.filter_by(order_number=paragraph.order_number) \
        .filter_by(path=parentPath(paragraph)).first()
    pattrs = json.loads(Pnode.attrs)
    if 'class' in pattrs.keys():
        paragraphClass = pattrs['class'][0]
        if ('TOC' in paragraphClass or '目录' in paragraphClass) and zh_count < 30:
            return False
        if ('Heading' in paragraphClass or '标题' in paragraphClass) and zh_count < 20:
            return False
        if 'Header' in paragraphClass and zh_count < 20:
            return False
        if 'Caption' in paragraphClass and zh_count < 40:
            return False
        if 'List-Paragraph' in paragraphClass and zh_count < 20:
            return False
    if 'style' in pattrs.keys():
        paragraphStyle = pattrs['style']
        if 'text-align:center' in paragraphStyle and zh_count < 30:
            return False
    spanAttrs = json.loads(paragraph.attrs)
    if 'style' in spanAttrs.keys():
        spanStyle = spanAttrs['style']
        styleDict = style2dict(spanStyle)
        if 'font-family' in styleDict.keys() and 'font-size' in styleDict.keys():
            if '黑体' == styleDict['font-family'] and FontSize(styleDict['font-size']) >= 14 and zh_count < 25:
                return False
    return True


def FontSize(fontsize):
    return float(fontsize.replace('pt', '').replace('em', ''))


def is_pure_english(sentence):
    return all(char.isalpha() or char.isspace() for char in sentence)


def style2dict(spanStyle):
    styles = spanStyle.split(';')
    dict = {}
    for style in styles:
        if style:
            try:
                if ':' in style:
                    dict[style.split(':')[0]] = style.split(':')[1]
            except Exception as e:
                pass
    return dict


def generate_random_name():
    name_length = random.randint(10, 10)
    name = ''.join(random.choices(string.ascii_letters + string.digits, k=name_length))
    return name


def cut_sentences(article):
    if not isinstance(article, str):
        article = str(article)
    zh_count, zh_content, english_words = count_zh(article)
    if english_words > 0:
        pass
    else:
        pass
    content = []
    paragraphs = article.split('\n')
    paragraph_content = []
    paragraph_index = 0
    sign = len(paragraphs)
    abstract_sign = 0
    index_sign = len(paragraphs)
    has_index = 0
    char_index = 0
    for index, paragraph in enumerate(paragraphs):
        if is_pure_english(paragraph):
            paragraph = paragraph.replace(' ', '')
        if paragraph[0:2] == '摘要' or paragraph[0:4] == '中文摘要' or paragraph[0:4] == '【摘要】' or paragraph[
                                                                                                     0:4] == '[摘要]':
            abstract_sign = index
    for index, paragraph in enumerate(paragraphs):
        paragraph_length = len(paragraph.strip())
        if paragraph_length > 0:
            char_index += paragraph_length
            paragraph = paragraph.replace('【', '[').replace('】', ']').replace('［', '').replace('］', '')
            paragraph_content.append(paragraph)
            ckwx_sign = ['[M]', '[N]', '[J]', '[P]', '[Z]', '[G]', '[O]', '[S]', '[D]', '[R]', '[K]', '[W]', '[B]',
                         '[A]', '[Q]', '[L]', '[V]', '[C]', '[T]', '[I]', '[Y]', '[H]', '[E]']

            zh_count, zh_content, english_words = count_zh(paragraph)
            if zh_content == '目录' and paragraph_length < 140:
                if has_index == 0:
                    index_sign = char_index
                has_index = 1
                content.append(
                    {
                        'content': paragraph,
                        'paragraph_index': paragraph_index,
                        'check': 0
                    }
                )
                paragraph_index += 1
                continue
            if char_index - index_sign <= 80 and re.search(
                    '((\(|（){1}(一|二|三|四|五|六|七|八|九|\d{1,2})(\)|）){1})|((一|二|三|四|五|六|七|八|九|\d{1,2})(.|。|,| ){1})',
                    paragraph) and len(zh_content) < 35 and len(paragraph) < 140:
                index_sign = char_index
                content.append(
                    {
                        'content': paragraph,
                        'paragraph_index': paragraph_index,
                        'check': 0
                    }
                )
                paragraph_index += 1
                continue
            if re.search(
                    '(([(（]){1}(一|二|三|四|五|六|七|八|九|\d{1,2})([)）]){1})|((一|二|三|四|五|六|七|八|九|\d{1,2})(.|。|,| ){1})',
                    paragraph) and len(zh_content) < 18 and len(paragraph) < 40:
                index_sign = char_index
                content.append(
                    {
                        'content': paragraph,
                        'paragraph_index': paragraph_index,
                        'check': 0
                    }
                )
                paragraph_index += 1
                continue
            if zh_count == 0 and len(paragraph) < 140:
                content.append(
                    {
                        'content': paragraph,
                        'paragraph_index': paragraph_index,
                        'check': 0
                    }
                )
                paragraph_index += 1
                continue
            if index < abstract_sign and zh_count < 140:
                content.append(
                    {
                        'content': paragraph,
                        'paragraph_index': paragraph_index,
                        'check': 0
                    }
                )
                paragraph_index += 1
                continue
            if zh_content == '参考文献' and paragraph_length < 140:
                sign = index
                content.append(
                    {
                        'content': paragraph,
                        'paragraph_index': paragraph_index,
                        'check': 0
                    }
                )
                paragraph_index += 1
                continue
            if ('关键词:' in paragraph or '关键词：' in paragraph) and paragraph_length < 40 and paragraph_length < 140:
                content.append(
                    {
                        'content': paragraph,
                        'paragraph_index': paragraph_index,
                        'check': 0
                    }
                )
                paragraph_index += 1
                continue
            if index > sign and (check_str(ckwx_sign, paragraph)) or (
                    re.search('([\[|【|［]\d{1,3}[】|\]|］])', paragraph.strip()[0:4])) and paragraph_length < 140:
                content.append(
                    {
                        'content': paragraph,
                        'paragraph_index': paragraph_index,
                        'check': 0
                    }
                )
                paragraph_index += 1
                continue
            if ('本人所呈交的毕业论文' in paragraph or '本人郑重声明' in paragraph) and len(paragraph) < 200:
                content.append(
                    {
                        'content': paragraph,
                        'paragraph_index': paragraph_index,
                        'check': 0
                    }
                )
                paragraph_index += 1
                continue
            if len(paragraph.strip()) <= 55 and paragraph_length > 0:
                content.append(
                    {
                        'content': paragraph,
                        'paragraph_index': paragraph_index,
                        'check': 1
                    }
                )
                paragraph_index += 1
            else:
                paragraph = paragraph.strip()
                sentence_list = SplitSentences('([。！；;])', paragraph)
                for sentence_list_item in sentence_list:
                    if len(sentence_list_item) > 55:
                        sub_sentences_list = SplitSentences('([,，])', sentence_list_item)
                        paragraph_sentence_list = merge_sub_sentence(sub_sentences_list)
                        for paragraph_sentence_list_item in paragraph_sentence_list:
                            content.append(
                                {
                                    'content': paragraph_sentence_list_item,
                                    'paragraph_index': paragraph_index,
                                    'check': 1
                                }
                            )
                    else:
                        if len(sentence_list_item) > 0:
                            content.append(
                                {
                                    'content': sentence_list_item,
                                    'paragraph_index': paragraph_index,
                                    'check': 1
                                }
                            )
                paragraph_index += 1
    return content, paragraph_content


def SplitSentences(splitstring, sentecne):
    sentences = re.split(splitstring, sentecne)
    sentences.append("")
    sentences = ["".join(i) for i in zip(sentences[0::2], sentences[1::2])]
    return sentences


def merge_sub_sentence(sentence_list):
    sentences_res = []
    sentences = ''.join(sentence_list)
    count_list = []
    sum = 0
    if len(sentence_list) == 1:
        return sentence_list
    else:
        start = 0
        index = 0
        mark = []
        for item in sentence_list:
            count_list.append(len(item))
            sum += len(item)
        for char in count_list:
            start = index
            index += char
            mark.append(index - 1)
        if 60 > sum >= 50:
            str = [''.join(sentence_list)]
            return str
        else:
            slice = math.ceil(sum / 50)
            average_base = math.ceil(sum / slice)
            average_list = []
            start = 0
            for i in range(1, slice):
                average_list.append(i * average_base)
            for average_item in average_list:
                target = nearest(mark, average_item)
                sentences_res.append(sentences[start:target + 1])
                start = target + 1
            sentences_res.append(sentences[start:len(sentences)])
        return sentences_res


def SumPreItem(n, list):
    sum = 0
    for i in range(0, n):
        sum += list[i]
    return sum


def nearest(mark, average):
    min = 1000
    target = 0
    for i in mark:
        dst = abs(average - i)
        if dst < min:
            min = dst
            target = i
    return target


def giftorder(order_number):
    free = ''
    for number in order_number:
        num = int(number)
        num = num + 1 if num >= 0 and num <= 8 else 0
        free += str(num)
    return free


def recovergiftorder(order_number):
    free = ''
    for number in order_number:
        num = int(number)
        num = num - 1 if num >= 1 and num <= 9 else 9
        free += str(num)
    return free


def getNonRepeatList(data):
    return [i for n, i in enumerate(data) if i not in data[:n]]


def PercentToFloat(percentStr):
    p_float = float(percentStr.strip("%")) / 100
    return p_float


def MaxAndIndex(list):
    tmp = []
    for item in list:
        timo = PercentToFloat(item)
        tmp.append(timo)
    index = tmp.index(max(tmp))
    return index, list[index]


def get_number(str):
    return int(str.replace('总字数：', '').replace(',', ''))


def get_float(str):
    return float(str.replace('%', '')) / 100


def get_token():
    md5str = str(time.time())
    m1 = hashlib.md5()
    m1.update(md5str.encode("utf-8"))
    token = m1.hexdigest()
    return token


def check_str(list, text):
    for i in list:
        if str(i) in text:
            return True
    return False


def check_doc_str(text):
    list = ['[M]', '[N]', '[J]', '[P]', '[Z]', '[G]', '[O]', '[S]', '[D]', '[R]', '[K]', '[W]', '[B]',
            '[A]', '[Q]', '[L]', '[V]', '[C]', '[T]', '[I]', '[Y]', '[H]', '[E]']
    for i in list:
        if str(i) in text:
            return True
    return False


def get_key_words(content):
    bot = TextRank(content)
    words = bot.get_n_keywords(18)
    list = []
    for item in words:
        list.append(item[0])
    return ' '.join(list)


def count_english_words(sentence):
    # 使用正则表达式找到所有的英文单词
    words = re.findall(r'\b[a-zA-Z]+\b', sentence)
    return len(words)


def count_zh(paragraph):
    count = 0
    zh = ''
    english_words = 0
    for s in paragraph:
        if '\u4e00' <= s <= '\u9fa5':
            count += 1
            zh += s
    if count == 0:
        english_words = count_english_words(paragraph)
    return count, zh, english_words


def sort_by_words(list):
    a_dict = {}
    for i in list:
        a_dict[i] = list.count(i)
    res = sorted(a_dict.items(), key=lambda x: x[1], reverse=True)
    list = []
    for item in res:
        list.append(item[0])
    return list


def parseDocument(file, order_number):
    filename = file.split(DOC_PATH)[1]
    convertUrl = f'{ConvertUrl}?filename={filename}&order_number={order_number}'
    res = requests.get(convertUrl)
    print(res.content)


def parseDoc(file):
    if '.docx' in file:
        content = ''
        document = Document(file)
        all_paragraphs = document.paragraphs
        for paragraph in all_paragraphs:
            if len(paragraph.text) > 0:
                content += paragraph.text + '\n'
        return content
    else:
        result = subprocess.run(['antiword', file], stdout=subprocess.PIPE)
        return result.stdout.decode('utf-8')


def get_file_md5(file):
    with open(file, 'rb') as fp:
        data = fp.read()
    file_md5 = hashlib.md5(data).hexdigest()
    return file_md5


def count_char_sum(content):
    char_sum =0
    paragraphs = content.split('\n')
    for paragraph in paragraphs:
        if is_english_paragraph(paragraph):
            count = len(paragraph.split(' '))
            char_sum += count
        else:
            clean_content = paragraph.replace(' ','').replace('\n', '')
            char_sum += len(clean_content)
    return char_sum
        
        
def is_english_paragraph(paragraph):
    for s in paragraph:
        if '\u4e00' <= s <= '\u9fa5':
            return False
    return True

# def send_report(order_number):
#     # 远程服务器的地址和端口
#     hostname = '118.195.199.198'
#     port = 22
#
#     # 远程服务器的用户名和密码
#     username = 'ubuntu'
#     password = 'Aa19950218!'
#
#     # 远程文件路径和本地文件路径
#     local_path = '/mnt/report/samereport_' + order_number + '.zip'
#     remote_path = '/mnt/report/samereport_' + order_number + '.zip'
#
#     # 创建 SSH 客户端
#     ssh_client = paramiko.SSHClient()
#
#     # 自动添加远程服务器的主机密钥
#     ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
#
#     # 连接到远程服务器
#     ssh_client.connect(hostname, port=port, username=username, password=password)
#
#     # 创建 SCP 客户端
#     scp_client = ssh_client.open_sftp()
#
#     # 从远程服务器下载文件到本地系统
#     scp_client.get(remote_path, local_path)
#
#     # 关闭 SCP 客户端和 SSH 客户端
#     scp_client.close()
#     ssh_client.close()
#     target_dir = '/mnt/report/'
#     with zipfile.ZipFile('/mnt/report/samereport_' + order_number + '.zip', 'r') as zip_ref:
#         # 解压缩所有文件到指定目录
#         zip_ref.extractall(target_dir)

if __name__ == '__main__':
    content = '''

（4） Financial work requires attention to detail and patience, as well as a high sense of responsibility and professional expertise. In daily work, we may encounter some problems or shortcomings that need to be carefully summarized and improved. Some people sometimes lack emphasis on the accuracy and timeliness of data in their financial work. In the process of processing financial data, due to busyness or negligence, data input errors or situations may occur, which not only affects the authenticity and reliability of financial statements, but also brings a certain degree of uncertainty to the company's decision-making. There is still a problem of insufficient understanding of financial policies and regulations in financial work. In enterprise financial work, financial policies and regulations are regulations that financial staff must strictly abide by, which has a great impact on financial staff. Carefully and meticulously carry out daily accounting and accounting supervision work such as accounting reimbursement, work distribution, review, bookkeeping, binding, verification, and statistics of original accounting vouchers.

    '''
    print(count_char_sum(content))
